/*
 * This file is part of shared-drawing.
 * shared-drawing is free software: you can redistribute it and/or modify it 
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option )
 * any later version. shared-drawing is distributed in the hope that it will
 * be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
 * Public License for more details.
 * You should have received a copy of the GNU General Public License along with 
 * shared-drawing. If not, see http://www.gnu.org/licenses/.
 */

package SharedDrawing;

import java.awt.Color;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

public class PrimitivesFactory 
{
	private ArrayList<Class> m_classes;
	
	public PrimitivesFactory(String packageName)
	{
		m_classes = new ArrayList<Class>();
		
        try 
        {
            this.findClasses(packageName);
            
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(PrimitivesFactory.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(PrimitivesFactory.class.getName()).log(Level.SEVERE, null, ex);
        }
	}
	
	public BoundingBox getInstance(String primitiveType) throws InstantiationException, IllegalAccessException
	{
		for(Class c : m_classes)
		{
			if(primitiveType.equals(c.getSimpleName()))
			{
				return (BoundingBox) c.newInstance();
			}
		}
		return null;
	}
	
	/* Les 2 methodes suivante permettent de recuperer les classes loaded d'un package */
	private void findClasses(String packageName) throws ClassNotFoundException, IOException 
	{
		ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
		
		assert classLoader != null;
		
		String path = packageName.replace('.', '/');
		Enumeration<URL> resources = classLoader.getResources(path);
		List<File> dirs = new ArrayList<File>();
		
		while (resources.hasMoreElements()) 
		{
			URL resource = resources.nextElement();
			dirs.add(new File(resource.getFile()));
		}
		
		for (File directory : dirs) 
		{
			m_classes.addAll(returnClasses(directory, packageName));
		}
	}

	private List<Class> returnClasses(File directory, String packageName) throws ClassNotFoundException 
	{
		List<Class> classes = new ArrayList<Class>();
		
		if (!directory.exists()) 
		{
			return classes;
		}
		
		File[] files = directory.listFiles();
		
		for (File file : files) 
		{
			if (file.isDirectory()) 
			{
				assert !file.getName().contains(".");
				classes.addAll(returnClasses(file, packageName + "." + file.getName()));
			} 
			else if (file.getName().endsWith(".class"))
			{
				classes.add(Class.forName(packageName + '.' + file.getName().substring(0, file.getName().length() - 6)));
			}
		}
		return classes;
	}
	
	public ArrayList<Class> getClasses()
	{
		return m_classes;
	}
	
	public Class getClass(int i)
	{
		return m_classes.get(i);
	}
	
	public String getClassName(int i)
	{
		return m_classes.get(i).getSimpleName();
	}
	
	public boolean isClassesEmpty()
	{
		if(m_classes.size() > 0)
		{
			return false;
		}
		return true;
	}
}

